package com.zg.autoflow.web;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.autoflow.dao.mapper.WorkflowInstanceMapper;
import com.zg.autoflow.req.SettleWorkflowDelegaterAddReq;
import com.zg.common.core.R;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowDelegaterDao;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowDelegater;
import com.zg.common.web.Req;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
* @author zg
*/
@RestController
@RequiredArgsConstructor
@Api(tags = {"代办管理"})
public class SettleWorkflowDelegaterController {

	private final SettleWorkflowDelegaterDao settleWorkflowDelegaterDao;

	@ApiOperation("代办人-指定代办人首页")
	@PostMapping("/delegater/getSettleWorkflowDelegater")
	public R<SettleWorkflowDelegater> getSettleWorkflowDelegaterMap() {
		String OPERATOR = String.valueOf(Req.getCurrUser().getId());
		LambdaQueryWrapper<SettleWorkflowDelegater> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SettleWorkflowDelegater::getOperator, OPERATOR);
		List<SettleWorkflowDelegater> list = settleWorkflowDelegaterDao.list(queryWrapper);
		SettleWorkflowDelegater settleWorkflowDelegater=null;
		if (CollectionUtil.isNotEmpty(list)){
			settleWorkflowDelegater=list.get(0);
		}
		return R.success(settleWorkflowDelegater);
	}


	@ApiOperation("添加或者修改代办")
	@PostMapping("/delegater/addSettleWorkflowDelegater")
	public R saveSettleWorkflowDelegater(@RequestBody@Valid SettleWorkflowDelegaterAddReq settleWorkflowDelegater) {
		SettleWorkflowDelegater settleWorkflowDelegater1 = new SettleWorkflowDelegater();
		String effectiveBeginDate = settleWorkflowDelegater.getEffectiveBeginDate();
		String effectiveEndDate = settleWorkflowDelegater.getEffectiveEndDate();
		BeanUtil.copyProperties(settleWorkflowDelegater,settleWorkflowDelegater1);
		settleWorkflowDelegater1.setOperator(String.valueOf(Req.getCurrUser().getId()));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// 解析字符串为Date对象
			Date parsedDate = dateFormat.parse(effectiveEndDate);
			// 将Date转换为Timestamp
			Timestamp timestamp = new Timestamp(parsedDate.getTime());
			settleWorkflowDelegater1.setEffectiveEndDate(timestamp);
			System.out.println("Timestamp: " + timestamp);
		} catch (Exception e) {
		}
		try {
			// 解析字符串为Date对象
			Date parsedDate = dateFormat.parse(effectiveBeginDate);
			// 将Date转换为Timestamp
			Timestamp timestamp = new Timestamp(parsedDate.getTime());
			settleWorkflowDelegater1.setEffectiveBeginDate(timestamp);
		} catch (Exception e) {
		}
		settleWorkflowDelegaterDao.saveOrUpdate(settleWorkflowDelegater1);
		return R.success();
	}

}
